/// 短视频数据传输对象
/// 
/// 对应Android原生的 ApiShortVideoDto
/// 用于短视频列表、短视频详情等场景
class ApiShortVideoDto {
  // 基本信息
  final int shortVideoId;        // 短视频ID
  final int userId;              // 用户ID
  final String? username;        // 用户名
  final String? avatar;          // 头像
  final String? content;         // 视频描述
  
  // 视频信息
  final String? videoUrl;        // 视频地址
  final String? thumb;           // 封面图
  final int width;               // 视频宽度
  final int height;              // 视频高度
  final int videoTime;           // 视频时长(秒)
  
  // 统计数据
  final int viewNum;             // 观看次数
  final int likeNum;             // 点赞数
  final int commentNum;          // 评论数
  final int shareNum;            // 分享数
  
  // 状态信息
  final int isLike;              // 是否已点赞 0:未点赞 1:已点赞
  final int isPrivate;           // 是否私密 0:公开 1:私密
  final double coin;             // 金币价格
  
  // 分类信息
  final String? classifyId;      // 分类ID
  final String? classifyName;    // 分类名称
  
  // 地理信息
  final String? province;        // 省
  final String? city;            // 市
  final String? address;         // 详细地址
  final double? lat;             // 纬度
  final double? lng;             // 经度
  
  // 时间信息
  final String? createTime;      // 创建时间
  final String? updateTime;      // 更新时间
  
  // 其他信息
  final String? musicId;         // 音乐ID
  final String? musicName;       // 音乐名称
  final int? productId;          // 商品ID
  final List<String>? images;    // 图片列表(如果是图片类型)
  final int? type;               // 类型 1:视频 2:图片
  
  ApiShortVideoDto({
    required this.shortVideoId,
    required this.userId,
    this.username,
    this.avatar,
    this.content,
    this.videoUrl,
    this.thumb,
    this.width = 0,
    this.height = 0,
    this.videoTime = 0,
    this.viewNum = 0,
    this.likeNum = 0,
    this.commentNum = 0,
    this.shareNum = 0,
    this.isLike = 0,
    this.isPrivate = 0,
    this.coin = 0.0,
    this.classifyId,
    this.classifyName,
    this.province,
    this.city,
    this.address,
    this.lat,
    this.lng,
    this.createTime,
    this.updateTime,
    this.musicId,
    this.musicName,
    this.productId,
    this.images,
    this.type,
  });
  
  /// 从JSON创建
  factory ApiShortVideoDto.fromJson(Map<String, dynamic> json) {
    return ApiShortVideoDto(
      shortVideoId: _parseInt(json['shortVideoId'] ?? json['id']),
      userId: _parseInt(json['userId'] ?? json['uid']),
      username: json['username'] as String?,
      avatar: json['avatar'] as String?,
      content: json['content'] as String?,
      videoUrl: json['videoUrl'] ?? json['href'],
      thumb: json['thumb'] as String?,
      width: _parseInt(json['width']),
      height: _parseInt(json['height']),
      videoTime: _parseInt(json['videoTime']),
      viewNum: _parseInt(json['viewNum']),
      likeNum: _parseInt(json['likeNum'] ?? json['zanNum']),
      commentNum: _parseInt(json['commentNum']),
      shareNum: _parseInt(json['shareNum']),
      isLike: _parseInt(json['isLike'] ?? json['isZan']),
      isPrivate: _parseInt(json['isPrivate']),
      coin: _parseDouble(json['coin']),
      classifyId: json['classifyId']?.toString(),
      classifyName: json['classifyName'] as String?,
      province: json['province'] as String?,
      city: json['city'] as String?,
      address: json['address'] as String?,
      lat: _parseDouble(json['lat']),
      lng: _parseDouble(json['lng']),
      createTime: json['createTime'] as String?,
      updateTime: json['updateTime'] as String?,
      musicId: json['musicId']?.toString(),
      musicName: json['musicName'] as String?,
      productId: json['productId'] != null ? _parseInt(json['productId']) : null,
      images: json['images'] != null 
          ? (json['images'] as String).split(',').where((s) => s.isNotEmpty).toList()
          : null,
      type: json['type'] != null ? _parseInt(json['type']) : null,
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'shortVideoId': shortVideoId,
      'userId': userId,
      'username': username,
      'avatar': avatar,
      'content': content,
      'videoUrl': videoUrl,
      'thumb': thumb,
      'width': width,
      'height': height,
      'videoTime': videoTime,
      'viewNum': viewNum,
      'likeNum': likeNum,
      'commentNum': commentNum,
      'shareNum': shareNum,
      'isLike': isLike,
      'isPrivate': isPrivate,
      'coin': coin,
      'classifyId': classifyId,
      'classifyName': classifyName,
      'province': province,
      'city': city,
      'address': address,
      'lat': lat,
      'lng': lng,
      'createTime': createTime,
      'updateTime': updateTime,
      'musicId': musicId,
      'musicName': musicName,
      'productId': productId,
      'images': images?.join(','),
      'type': type,
    };
  }
  
  /// 辅助方法: 安全解析int
  static int _parseInt(dynamic value) {
    if (value == null) return 0;
    if (value is int) return value;
    if (value is double) return value.toInt();
    if (value is String) return int.tryParse(value) ?? 0;
    return 0;
  }
  
  /// 辅助方法: 安全解析double
  static double _parseDouble(dynamic value) {
    if (value == null) return 0.0;
    if (value is double) return value;
    if (value is int) return value.toDouble();
    if (value is String) return double.tryParse(value) ?? 0.0;
    return 0.0;
  }
  
  /// 是否已点赞
  bool get liked => isLike == 1;
  
  /// 是否私密视频
  bool get isPrivateVideo => isPrivate == 1;
  
  /// 是否付费视频
  bool get isPaid => coin > 0;
  
  /// 格式化视频时长
  String get formattedDuration {
    final minutes = videoTime ~/ 60;
    final seconds = videoTime % 60;
    return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  }
  
  /// 格式化观看次数
  String get formattedViewNum {
    if (viewNum >= 10000) {
      return '${(viewNum / 10000).toStringAsFixed(1)}万';
    }
    return viewNum.toString();
  }
  
  /// 格式化点赞数
  String get formattedLikeNum {
    if (likeNum >= 10000) {
      return '${(likeNum / 10000).toStringAsFixed(1)}万';
    }
    return likeNum.toString();
  }
  
  /// 复制并修改点赞状态
  ApiShortVideoDto copyWithLike(bool liked) {
    return ApiShortVideoDto(
      shortVideoId: shortVideoId,
      userId: userId,
      username: username,
      avatar: avatar,
      content: content,
      videoUrl: videoUrl,
      thumb: thumb,
      width: width,
      height: height,
      videoTime: videoTime,
      viewNum: viewNum,
      likeNum: liked ? likeNum + 1 : likeNum - 1,
      commentNum: commentNum,
      shareNum: shareNum,
      isLike: liked ? 1 : 0,
      isPrivate: isPrivate,
      coin: coin,
      classifyId: classifyId,
      classifyName: classifyName,
      province: province,
      city: city,
      address: address,
      lat: lat,
      lng: lng,
      createTime: createTime,
      updateTime: updateTime,
      musicId: musicId,
      musicName: musicName,
      productId: productId,
      images: images,
      type: type,
    );
  }
}

